'use strict'
import {
  Model,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  CreationOptional,
} from 'sequelize'
import dayjs from 'dayjs'
import { Models } from '../type'
import sequelize from './index'

class Log extends Model<InferAttributes<Log>, InferCreationAttributes<Log>> {
  declare id: CreationOptional<number>
  declare level: string
  declare message: string
  declare meta: string
  declare timestamp: Date

  static associate(models: Models) {}
}
Log.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    level: DataTypes.STRING,
    message: DataTypes.STRING,
    meta: DataTypes.STRING,
    timestamp: {
      type: DataTypes.DATE,
      get() {
        return this.getDataValue('timestamp')
          ? dayjs(this.getDataValue('timestamp')).format('YYYY-MM-DD HH:mm:ss')
          : null
      },
    },
  },
  {
    sequelize,
    modelName: 'Log',
    timestamps: false,
  },
)

export default Log
